﻿Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory

    ''' <summary>
    ''' A class that caches the dungeons and bosses available on the Armory
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Dungeons
        Inherits WowArmoryXML

        Private dsPage As New DungeonSchema.page

        ''' <summary>
        ''' The URL of the dungeon and boss data
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL() As String
            Get
                Return "http://www.wowarmory.com/data/dungeonStrings.xml"
            End Get
        End Property

        ''' <summary>
        ''' Returns a list of available dungeons
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Dungeons() As List(Of Dungeon)
            Get
                If IsNothing(dsPage) Then
                    Return New List(Of Dungeon)
                End If
                Dim ddData As New DungeonData
                Return (From a In dsPage.dungeons(0).dungeon Select New Dungeon(CBool(If((From d In ddData.Dungeons Where d.ID = a.id).Count = 0, False, (From d In ddData.Dungeons Where d.ID = a.id).First().HasHeroic)), a.id, a.name, CType(a.release, ReleaseType))).ToList()
            End Get
        End Property

        ''' <summary>
        ''' Returns a list of available bosses
        ''' </summary>
        ''' <param name="intDungeonID"></param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Bosses(ByVal intDungeonID As Integer) As List(Of Boss)
            Get
                If IsNothing(dsPage) Then
                    Return New List(Of Boss)
                End If
                Return (From a In dsPage.dungeons(0).dungeon.Where(Function(d) d.id = intDungeonID).First.boss Select New Boss(a.id, a.name)).ToList()
            End Get
        End Property

        ''' <summary>
        ''' Constructor that retrieves the dungeon and boss data from either the armory or the local cache
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            If IsNothing(Cache.Cache("LibWowArmory.Dungeons")) Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(dsPage.GetType())

                Dim srXML As New StringReader(MyBase.strXML)

                dsPage = CType(xsSerializer.Deserialize(srXML), DungeonSchema.page)

                Cache.Cache.Insert("LibWowArmory.Dungeons", dsPage, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.NotRemovable, Nothing)
            Else
                dsPage = CType(Cache.Cache("LibWowArmory.Dungeons"), DungeonSchema.page)
            End If
        End Sub

        ''' <summary>
        ''' A class that encapsulates a single dungeon returned from the Armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Class Dungeon

            Private blnHasHeroic As Boolean
            Private intID As Integer
            Private strName As String
            Private rRelease As ReleaseType

            ''' <summary>
            ''' Determines if the dungeon has an heroic version
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property HasHeroic() As Boolean
                Get
                    Return blnHasHeroic
                End Get
                Set(ByVal value As Boolean)
                    blnHasHeroic = value
                End Set
            End Property

            ''' <summary>
            ''' The dungeon's identification number
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property ID() As Integer
                Get
                    Return intID
                End Get
                Set(ByVal value As Integer)
                    intID = value
                End Set
            End Property

            ''' <summary>
            ''' The name of the dungeon
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Name() As String
                Get
                    Return strName
                End Get
                Set(ByVal value As String)
                    strName = value
                End Set
            End Property

            ''' <summary>
            ''' The release of World of Warcraft the dungeon was introduced in
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Release() As ReleaseType
                Get
                    Return rRelease
                End Get
                Set(ByVal value As ReleaseType)
                    rRelease = value
                End Set
            End Property

            ''' <summary>
            ''' Constructor to create a new dungeon object
            ''' </summary>
            ''' <param name="intID"></param>
            ''' <param name="strName"></param>
            ''' <param name="rRelease"></param>
            ''' <remarks></remarks>
            Friend Sub New(ByVal blnHasHeroic As Boolean, ByVal intID As Integer, ByVal strName As String, ByVal rRelease As ReleaseType)
                Me.blnHasHeroic = blnHasHeroic
                Me.intID = intID
                Me.strName = strName
                Me.rRelease = rRelease
            End Sub

        End Class

        ''' <summary>
        ''' A class that encapsulates a single boss returned from the Armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Class Boss

            Private intID As Integer
            Private strName As String

            ''' <summary>
            ''' The boss's identification number
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property ID() As Integer
                Get
                    Return intID
                End Get
                Set(ByVal value As Integer)
                    intID = value
                End Set
            End Property

            ''' <summary>
            ''' The name of the boss
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Name() As String
                Get
                    Return strName
                End Get
                Set(ByVal value As String)
                    strName = value
                End Set
            End Property

            ''' <summary>
            ''' Constructor to create a new boss object
            ''' </summary>
            ''' <param name="intID"></param>
            ''' <param name="strName"></param>
            ''' <remarks></remarks>
            Friend Sub New(ByVal intID As Integer, ByVal strName As String)
                Me.intID = intID
                Me.strName = strName
            End Sub

        End Class

    End Class

End Namespace